package com.example.shushiyunsst.fragment.sigin

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.view.View
import android.widget.SeekBar
import android.widget.Toast
import com.alibaba.fastjson.JSON
import com.bumptech.glide.request.target.CustomViewTarget
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition
import com.example.library_common.base.BaseDataBindingFragment
import com.example.library_common.base.LazyFragment
import com.example.library_common.glidmodule.GlideApp
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.widget.verification.OnNewCaptchaMatchCallback
import com.example.shushiyunsst.widget.verification.new_view.NewSwipeCaptchaView
import com.example.shushiyunsst.widget.verification.new_view.NewVerificationListener
import com.example.shushiyunsst.widget.verification.new_view.VerificationViewBean
import com.example.shushiyunsst.widget.verification.view.SwipeCaptchaView
import com.example.shushiyunsst.widget.verification.view.VerificationListener
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.impl.FullScreenPopupView

/**
 * 注册图形验证
 * */
class SignUpVerifyFragment(context: Context):
    FullScreenPopupView(context), OnNewCaptchaMatchCallback,
    SeekBar.OnSeekBarChangeListener {

    companion object {
//        fun newInstance(): SignUpVerifyFragment {
//            val args = Bundle()
//            val fragment = SignUpVerifyFragment()
//            fragment.arguments = args
//            return fragment
//        }
    }

    override fun getImplLayoutId(): Int {
        return R.layout.sign_up_verify_fragment
    }

//    override fun layoutId() = R.layout.sign_up_verify_fragment

    private var mVerificationListener: NewVerificationListener? = null

    private var bean = VerificationViewBean()

    private var requestTime = ""

    private lateinit var loadingStatus:View

    private lateinit var mSeekBar:SeekBar

    private lateinit var requestVerifyCoding:View

    private lateinit var mSwipeCaptchaView: NewSwipeCaptchaView

    private lateinit var imgVerifyCoding:View

    fun getVerificationViewBean() = bean

    fun setVerificationListener(mVerificationListener: NewVerificationListener?): SignUpVerifyFragment {
        this.mVerificationListener = mVerificationListener
        return this
    }

    override fun onCreate() {
        super.onCreate()
        loadingStatus =findViewById(R.id.loading)
        mSwipeCaptchaView =findViewById(R.id.mSwipeCaptchaView)
        mSeekBar =findViewById(R.id.mSeekBar)
        requestVerifyCoding =findViewById(R.id.requestVerifyCoding)
        imgVerifyCoding=findViewById(R.id.imgVerifyCoding)
        requestVerifyCoding.setOnClickListener {
            getImageVerifyCode()
        }
        mSwipeCaptchaView.setOnCaptchaMatchCallback(this)
        mSeekBar.setOnSeekBarChangeListener(this)
        getImageVerifyCode()
    }

//    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        super.onViewCreated(view, savedInstanceState)
//
//
//        view.setOnClickListener { onBackPressedSupport() }
//
////        viewBinder.loadingStatus.visibility=View.VISIBLE
////        viewBinder.imgVerifyCoding.visibility=View.INVISIBLE
//        viewBinder.mSwipeCaptchaView.measure(0,0)
//        viewBinder.mSwipeCaptchaView.setOnCaptchaMatchCallback(this)
//        viewBinder.mSeekBar.setOnSeekBarChangeListener(this)
//        viewBinder.requestVerifyCoding.setOnClickListener {
//            getImageVerifyCode()
//        }
//
//        getImageVerifyCode()
//    }

    private fun getImageVerifyCode() {

        requestTime = System.currentTimeMillis().toString()

        RestClient.builder()
            .url(R.string.getImageVerifyCode)
            .raw(JSON.toJSONString(mapOf("hashCode" to requestTime)))
            .success {
                parserVerifyCodeData(it)
            }
            .error { _, _ ->
                onFailed()
            }
            .failure {
                onFailed()
            }

            .build()
            .post()
    }

    private fun parserVerifyCodeData(it: String?) {
        bean = JSON.parseObject(it, VerificationViewBean::class.java)

        bean.requestTime = requestTime

        GlideApp.with(context)
            .asBitmap()
            .load(bean.imgUrl)
            .into(object :
                SimpleTarget<Bitmap>() {
                override fun onLoadFailed(errorDrawable: Drawable?) {
                    onFailed()
                }

                override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {

                    loadingStatus.visibility = View.GONE
                    imgVerifyCoding.visibility = View.VISIBLE

                    mSwipeCaptchaView.setImageBitmap(resource)
                    mSwipeCaptchaView.createCaptcha()
                }

//                override fun onResourceCleared(placeholder: Drawable?) {
//
//                }

            })
    }

    private fun onFailed() {
//        viewBinder.loadingStatus.visibility = View.VISIBLE
//        viewBinder.imgVerifyCoding.visibility = View.INVISIBLE
        loadingStatus.visibility = View.VISIBLE
        imgVerifyCoding.visibility = View.INVISIBLE
        Toast.makeText(context,"请求失败",Toast.LENGTH_SHORT).show()
//        onBackPressedSupport()
        dismiss()
    }

    override fun matchSuccess(swipeCaptchaView: NewSwipeCaptchaView?) {
        swipeCaptchaView?.resetCaptcha()
        mSeekBar.progress = 0
        mVerificationListener?.notifyStatusResult(VerificationListener.SUCCESS_STATUS)
    }

    override fun matchFailed(swipeCaptchaView: NewSwipeCaptchaView?) {
        swipeCaptchaView?.resetCaptcha()
        mSeekBar.progress = 0
        mVerificationListener?.notifyStatusResult(VerificationListener.FAIL_STATUS)
    }

    override fun onProgressChanged(p0: SeekBar?, progress: Int, p2: Boolean) {
         mSwipeCaptchaView.setCurrentSwipeValue(progress)
    }

    override fun onStartTrackingTouch(p0: SeekBar?) {
      mSeekBar.max = mSwipeCaptchaView.getMaxSwipeValue()
    }

    override fun onStopTrackingTouch(p0: SeekBar?) {
       mSwipeCaptchaView.matchCaptcha()
    }
}